<?php

/**
 * @property product $product
 * @property category_model $category_model
 * @property district $district
 * @property product_image $product_image
 * @property province $province
 * @property user_model $user_model
 * @property area_unit $area_unit
 * @property price_unit $price_unit
 * @property orientation $orientation
 * @property CI_DB_active_record $db
 * @property CI_DB_forge $dbforge
 * @property CI_Benchmark $benchmark
 * @property CI_Calendar $calendar
 * @property CI_Cart $cart
 * @property CI_Config $config
 * @property CI_Controller $controller
 * @property CI_Email $email
 * @property CI_Encrypt $encrypt
 * @property CI_Exceptions $exceptions
 * @property CI_Form_validation $form_validation
 * @property CI_Ftp $ftp
 * @property CI_Hooks $hooks
 * @property CI_Image_lib $image_lib
 * @property CI_Input $input
 * @property CI_Lang $lang
 * @property CI_Loader $load
 * @property CI_Log $log
 * @property CI_Model $model
 * @property CI_Output $output
 * @property CI_Pagination $pagination
 * @property CI_Parser $parser
 * @property CI_Profiler $profiler
 * @property CI_Router $router
 * @property CI_Session $session
 * @property CI_Sha1 $sha1
 * @property CI_Table $table
 * @property CI_Trackback $trackback
 * @property CI_Typography $typography
 * @property CI_Unit_test $unit_test
 * @property CI_Upload $upload
 * @property CI_URI $uri
 * @property CI_User_agent $user_agent
 * @property CI_Xmlrpc $xmlrpc
 * @property CI_Xmlrpcs $xmlrpcs
 * @property CI_Zip $zip
 * @property CI_Javascript $javascript
 * @property CI_Jquery $jquery
 * @property CI_Utf8 $utf8
 * @property CI_Security $security
 * @property slideshow_model $slideshow_model
 * @property show_style_model $show_style_model
 */

class MY_model extends CI_Model
{
    public $cache;
    public $table_name;
    public $deleted_by;
    public $updated_by;
    public $created_by;
    public $inserted_date;
    public $updated_date;
    public $deleted_date;

    public function __construct()
    {
        parent::__construct();
        $this->cache = new stdClass();
    }

    // Private method
    public function __get($key)
    {
        if (method_exists($this, $MethodName = 'get_' . $key)) return $this->$MethodName();
        $CI =& get_instance();
        return $CI->$key;
    }

    public static function getDbInstance()
    {
        $ci_ins = & get_instance();
        return $ci_ins->db;
    }


//----------------- INERT DATA TO DATABASE
    public function delete_data($where)
    {
        $this->getDbInstance()->delete($this->table_name,$where);
//        $this->prepare_delete_data();
//        $this->getDbInstance()->update($this->table_name,array('deleted_date' => time()),$where);
    }

    public function insert_data()
    {
        $this->prepare_insert_data();
        $this->getDbInstance()->insert($this->table_name,$this->to_array());

        // Insert to tracking version table
    }

    public function update_data($where)
    {
//        $this->prepare_update_data();
        $this->getDbInstance()->update($this->table_name,$this->to_array(),$where);
    }
//------------------------------------------



//----------------- TRACKING ACTION TO DATABASE
    private function tracking($table_name)
    {
        $tracking_data = array(
            'table_name'    =>  $table_name,
            'version'       =>  tracking_version_model::get_first_element_by_table_name($table_name)->version + 1
        );
        self::getDbInstance()->insert('tracking_version', $tracking_data);
    }
//------------------------------------------



//----------------- PREPARE DATA TO INSERT

    protected function prepare_data()
    {
        //$this->version = $this->version+1;
    }

    protected function prepare_insert_data()
    {
        $this->inserted_date = time();
    }

    private function prepare_update_data()
    {
        $this->updated_date = time();
    }

    public function prepare_delete_data()
    {
        $this->deleted_date = time();
    }

    protected function to_array()
    {
        return array();
    }
//------------------------------------------
}

